package org.itheima.googleplay.ui.adapter;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.itheima.googleplay.bean.AppInfoBean;
import org.itheima.googleplay.bean.DownloadBean;
import org.itheima.googleplay.manager.DownloadManager;
import org.itheima.googleplay.manager.DownloadManager.DownloadObserver;
import org.itheima.googleplay.ui.activity.AppDetailActivity;
import org.itheima.googleplay.ui.holder.AppItemHolder;
import org.itheima.googleplay.ui.holder.BaseHolder;
import org.itheima.googleplay.utils.UIUtils;

import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.RecyclerListener;
import android.widget.AdapterView;
import android.widget.Toast;

/**
 * @包名: org.itheima.googleplay.ui.adapter
 * @类名: AppListAdapter
 * @作者: 肖琦
 * @创建时间 : 2015-3-23 下午4:17:09
 * 
 * @描述: TODO
 * 
 * @SVN版本: $Rev: 23 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-26 15:02:40 +0800 (星期四, 26 三月 2015) $
 * 
 * @更新描述:
 */
public class AppListAdapter extends SuperBaseAdapter<AppInfoBean> implements DownloadObserver, RecyclerListener
{
	/**
	 * 当前listView对应的holder
	 */
	private List<AppItemHolder>	mHolders	= new LinkedList<AppItemHolder>();

	public AppListAdapter(AbsListView listView, List<AppInfoBean> datas) {
		super(listView, datas);

		// 设置listView的item回收时的监听
		listView.setRecyclerListener(this);
	}

	@Override
	protected BaseHolder<AppInfoBean> getHolder(int position)
	{
		AppItemHolder holder = new AppItemHolder();
		synchronized (mHolders)
		{
			mHolders.add(holder);
		}
		return holder;
	}

	@Override
	protected void onInnerItemClick(AdapterView<?> parent, View view, int position, long id)
	{

		AppInfoBean bean = mDatas.get(position);
		// Toast.makeText(UIUtils.getContext(), bean.name,
		// Toast.LENGTH_SHORT).show();

		Intent intent = new Intent(UIUtils.getContext(), AppDetailActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(AppDetailActivity.KEY_PACKAGE_NAME, bean.packageName);
		UIUtils.startActivity(intent);
	}

	/**
	 * 开启观察者
	 */
	public void startObserver()
	{
		DownloadManager.getInstance().addObserver(this);

		// UI先刷新

		ListIterator<AppItemHolder> iterator = mHolders.listIterator();
		while (iterator.hasNext())
		{
			AppItemHolder holder = iterator.next();

			holder.checkDownloadState();
		}
	}

	/**
	 * 停止观察者
	 */
	public void stopObserver()
	{
		DownloadManager.getInstance().deleteObserver(this);
	}

	@Override
	public void onDownloadStateChange(DownloadManager manager, DownloadBean data)
	{
		// 子线程中
		// 让holder 去根据状态改变UI

		safeRefreshHolders(data);

	}

	private void safeRefreshHolders(DownloadBean data)
	{
		ListIterator<AppItemHolder> iterator = mHolders.listIterator();
		while (iterator.hasNext())
		{
			AppItemHolder holder = iterator.next();

			holder.safeRefreshView(data);
		}

	}

	@Override
	public void onDownloadProgressChange(DownloadManager manager, DownloadBean data)
	{
		// 子线程中
		// 让holder 去根据状态改变UI
		safeRefreshHolders(data);

	}

	@Override
	public void onMovedToScrapHeap(View view)
	{
		Object tag = view.getTag();
		if (tag != null && tag instanceof AppItemHolder)
		{
			// 移除这个holder
			synchronized (mHolders)
			{
				mHolders.remove((AppItemHolder) tag);
			}
		}
	}
}
